class Solution:
    def decodeString(self, s: str) -> str:
        zhan = []
        num_set = {"0", "1", "2", "3", "4", "5", "6", "7", "8", "9"}
        for strs in s:
            if strs in num_set:
                if zhan and isinstance(zhan[-1], int):
                    zhan[-1] = zhan[-1] * 10 + int(strs)
                else:
                    zhan.append(int(strs))
            elif strs == "]":
                mid_str = ""
                while zhan[-1] != "[":
                    mid_str = zhan.pop(-1) + mid_str
                zhan.pop(-1)
                num = zhan.pop(-1)
                zhan.append(mid_str * num)
            else:
                zhan.append(strs)
        res = ""
        for strs in zhan:
            res += strs
        return res


a = Solution()
print(a.decodeString("3[a]2[bc]"))
print(a.decodeString("3[a2[c]]"))
print(a.decodeString("2[abc]3[cd]ef"))
